<template>
    <div class="table-page">
        <div class="table-operation-box">
            <el-button
                type="primary"
                style="margin-bottom: 2em"
                icon="el-icon-plus"
                @click="clickAdd('单个添加')"
                >单个添加
            </el-button>
            <el-button
                type="primary"
                style="margin-bottom: 2em"
                icon="el-icon-plus"
                @click="clickBatchAdd('批量添加')"
                >批量添加
            </el-button>
            <el-button
                type="primary"
                style="margin-bottom: 2em"
                icon="el-icon-arrow-right"
                @click="ipAll('IP列表')"
                >IP列表
            </el-button>
        </div>

        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
            >
                <el-table-column
                    prop="ip"
                    label="IP段"
                    width="300"
                ></el-table-column>
                <el-table-column
                    prop="startIp"
                    label="起始ip"
                ></el-table-column>
                <el-table-column prop="endIp" label="结束ip"></el-table-column>
                <el-table-column
                    prop="remark"
                    label="备注"
                    width="300"
                ></el-table-column>
                <el-table-column
                    label="操作"
                    min-width="200"
                    align="center"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            v-if="!scope.row.edit"
                            lass="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="clickSimpleEdit(scope.row, '修改机构IP')"
                            >编辑
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            lass="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-delete"
                            :loading="scope.row.opLoading"
                            @click="
                                tableDelete(
                                    '/user/sys/group/ip/delete',
                                    {
                                        id: scope.row.id
                                    },
                                    true
                                )
                            "
                            >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                background
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total,  prev, pager, next, sizes, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
        <el-dialog
            :title="dialogFormTitle"
            :visible.sync="dialogFormVisible"
            :before-close="dialogClose"
            :modal="false"
            custom-class="two-dialog"
        >
            <el-form
                ref="dataForm"
                :model="dataForm"
                label-width="8em"
                :rules="dataFormRule"
            >
                <el-form-item label="ip" prop="ip">
                    <el-input v-model="dataForm.ip"></el-input>
                </el-form-item>
                <!-- <el-form-item label="有效期" prop="endTime">
                    <el-date-picker
                        v-model="dataForm.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="IP有效期"
                    >
                    </el-date-picker>
                </el-form-item> -->
                <el-form-item label="备注" prop="remark">
                    <el-input
                        v-model="dataForm.remark"
                        type="textarea"
                        rows="5"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="dialogClose"
                    >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="ipSave()"
                    >保 存</el-button
                >
            </span>
        </el-dialog>
        <el-dialog
            :title="batchDialogFormTitle"
            :visible.sync="batchDialogFormVisible"
            :before-close="batchDialogClose"
            :modal="false"
            custom-class="two-dialog"
        >
            <el-form
                ref="batchDataForm"
                :model="batchDataForm"
                label-width="8em"
                :rules="batchDataFormRule"
            >
                <el-form-item label="添加IP" prop="ipList">
                    <el-input
                        v-model="batchDataForm.ipList"
                        placeholder="192.168.22.0/24,192.168.22.55"
                        type="textarea"
                        rows="5"
                        style="width:35em"
                    ></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input
                        v-model="batchDataForm.remark"
                        type="textarea"
                        rows="5"
                        style="width:35em"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="batchDialogClose"
                    >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="batchDataFormSave"
                    >保 存</el-button
                >
            </span>
        </el-dialog>
        <el-dialog
            :title="ipAllDialogFormTitle"
            :visible.sync="ipAllVisible"
            width="100%"
            :before-close="ipAllClose"
            :modal="false"
        >
            <div class="el-dialog-div">
                <group-ip-all-component v-if="ipAllVisible" :groupId="groupId">
                </group-ip-all-component>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {TablePage} from "@/layout/mixin/iVue.js";
import {mapState} from "vuex";
import {post} from "@/api/request";
import groupIpAllComponent from "./groupIpAll";

export default {
    name: "GroupIp",
    components: { groupIpAllComponent },
    mixins: [TablePage],
    props: {
        groupId: {
            type: Number,
            default: null
        }
    },

    data() {
        return {
            queryData: {
                groupId: this.groupId,
                code: "",
                enable: null
            },
            initAddForm: {
                ip: null,
                remark: null,
                endTime: null,
                groupId: this.groupId
            },
            dataFormRule: {
                ip: [
                    {
                        required: true,
                        message: "请输入起始Ip",
                        trigger: "blur"
                    }
                ]
            },
            batchInitDataForm: {
                groupId: this.groupId,
                ipList: "",
                remark: null
            },
            batchDataForm: {
                ipList: "",
                remark: null
            },
            batchDataFormRule: {
                ipList: [
                    {
                        required: true,
                        message: "IP不能为空",
                        trigger: "blur"
                    }
                ]
            },
            ipAllVisible: false,
            ipAllDialogFormTitle: null
        };
    },
    computed: {
        ...mapState({})
    },
    watch: {},
    created() {console.log(groupId)},
    mounted() {},
    methods: {
        search(queryData) {
            this.loadTable("/user/sys/group/ip/page", queryData, true);
        },
        ipSave() {
            this.formDataSave("/user/sys/group/ip/update");
        },
        compareIP(ipBegin, ipEnd) {
            if (this.isIpV4(ipBegin) && this.isIpV4(ipEnd)) {
                return this.compareIPV4(ipBegin, ipEnd);
            } else if (!this.isIpV4(ipBegin) && !this.isIpV4(ipEnd)) {
                return this.compareIPV6(ipBegin, ipEnd);
            } else {
                this.$confirm("请保持IP类型一致");
                return false;
            }
        },
        clickBatchAdd(title) {
            this.batchInitDataForm.ipList = "";
            this.batchDataForm = {
                ...this.batchInitDataForm,
                addAction: true
            };
            this.batchDialogFormVisible = true;
            this.batchDialogFormTitle = title;
        },
        batchDialogClose() {
            this.batchDataForm = {};
            this.batchDialogFormVisible = false;
        },
        batchDataFormSave() {
            this.$refs.batchDataForm.validate(valid => {
                if (valid) {
                    this.batchDataForm.ipList=this.batchDataForm.ipList.split("\n").filter(f => f !== "").map(m => m.trim()).join(",");
                    this.submitFlag = true;
                    post("/user/sys/group/ip/batchAdd", this.batchDataForm)
                        .then(() => {
                            this.batchDialogFormVisible = false;
                            this.search();
                        })
                        .finally(() => {
                            this.submitFlag = false;
                        });
                } else {
                    j();
                }
            });
        },

        ipAll(title) {
            this.ipAllVisible = true;
            this.ipAllDialogFormTitle = title;
        },
        ipAllClose() {
            this.ipAllVisible = false;
            this.search();
        }
    }
};
</script>

<style lang="scss" scoped>
.school-logo {
    .logo-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 108px;
        height: 108px;
        line-height: 108px;
        text-align: center;
    }

    .logo-img {
        width: 108px;
        height: 108px;
        display: block;
    }
}

.table-edit-from {
    .address-select {
        width: 8em;
        margin-right: 2em;
    }
}
</style>
<style>
.school-logo .logo-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.school-logo .logo-uploader .el-upload:hover {
    border-color: #409eff;
}
</style>
